import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:hitutor/UI/Account/EditPersonalData.dart';
import 'package:hitutor/UI/Account/Login.dart';
import 'package:hitutor/UI/Member/ApplyPause.dart';
import 'package:hitutor/UI/Member/MyLesson/ClassList.dart';
import 'package:hitutor/UI/Member/FavoriteTeacher/FavoriteTeacher.dart';
import 'package:hitutor/UI/Member/FriendList.dart';
import 'package:hitutor/UI/Member/HelpCenter.dart';
import 'package:hitutor/UI/Member/MyOrder/MyOrderList.dart';
import 'package:hitutor/UI/Member/SetInterest.dart';
import 'package:hitutor/Util/Global.dart';
import 'package:hitutor/Util/MyColor.dart';
import 'package:hitutor/Util/PageUtil.dart';
import 'package:hitutor/Widget/MyButton.dart';
import 'package:hitutor/Widget/MyImage.dart';

import '../../Util/HttpGo.dart';
import '../../Widget/MyDialog.dart';

class MemberBody extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MemberBody();
  }
}

class _MemberBody extends State<MemberBody> {
  logOut() {
    String registrationTokens = Global.firebasetoken.isEmpty ? Global.JPushRegToken : Global.firebasetoken;
    registrationTokens = Platform.isIOS ? Global.firebasetoken : registrationTokens;

    int pushProvider = Global.firebasetoken.isEmpty ? Global.Firebase : Global.JPush;
    pushProvider = Platform.isIOS ? Global.APN : pushProvider;

    HttpGo(context).post("/Login/Logout", {"registrationTokens": registrationTokens, "pushProvider": pushProvider}, (data) {
      PageUtil().openReplace(context, Login());
    }, (code, error) => showToast(error));
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Container(
          color: MyColor.F8,
          child: Column(children: [
            PersonInfo(),
            Container(
                color: MyColor.White,
                padding: EdgeInsets.fromLTRB(16, 12, 16, 8),
                child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                  Text("課程", style: TextStyle(fontSize: 14, color: MyColor.C66)),
                  Container(height: 8),
                  Column(children: [
                    ItemMenu("menu-alt3.png", "我的課程", onTap: () {
                      PageUtil().open(context, ClassList());
                    }),
                    ItemMenu("bag-alt.png", "我的訂單", onTap: () {
                      PageUtil().open(context, MyOrderList(key: Global.myOrderKey));
                    }),
                    ItemMenu("warning-alt2.png", "申請帳號暫停", onTap: () {
                      PageUtil().open(context, ApplyPause());
                    }),
                  ])
                ])),
            Container(
                color: MyColor.White,
                padding: EdgeInsets.fromLTRB(16, 12, 16, 8),
                margin: EdgeInsets.only(top: 8),
                child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                  Text("設定", style: TextStyle(fontSize: 14, color: MyColor.C66)),
                  Container(height: 8),
                  Column(children: [
                    ItemMenu("edit-user-5.png", "編輯個人資料", onTap: () {
                      PageUtil().open(context, EditPersonalData());
                    }),
                    ItemMenu("favourite.png", "最愛教師設定", onTap: () {
                      PageUtil().open(context, FavoriteTeacher());
                    }),
                    ItemMenu("add-user-4.png", "朋友圈", onTap: () {
                      PageUtil().open(context, FriendList());
                    }),
                    ItemMenu("file-favorite-6.png", "喜愛的主題", onTap: () {
                      PageUtil().open(context, SetInterest());
                    }),
                  ])
                ])),
            Container(
                color: MyColor.White,
                padding: EdgeInsets.fromLTRB(16, 12, 16, 8),
                margin: EdgeInsets.only(top: 8),
                child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                  Text("小幫手", style: TextStyle(fontSize: 14, color: MyColor.C66)),
                  Container(height: 8),
                  Column(children: [
                    ItemMenu("question.png", "幫助中心", onTap: () {
                      PageUtil().open(context, HelpCenter());
                    }),
                  ])
                ])),
            Button("登出帳號",
                height: 32, width: 260, bgColor: Color(0xff8ea7c5), fontSize: 14, margin: EdgeInsets.symmetric(vertical: 16), onTap: logOut)
          ])),
    );
  }
}

class PersonInfo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
        decoration: BoxDecoration(color: Color(0xff8ea7c5), borderRadius: BorderRadius.vertical(bottom: Radius.circular(12))),
        height: 80,
        width: double.infinity,
        margin: EdgeInsets.fromLTRB(24, 0, 24, 16),
        child: Stack(alignment: Alignment.center, children: [
          Positioned(
              top: 8,
              right: 8,
              child: LocalImage("settings.png", width: 24, onTap: () {
                PageUtil().open(context, EditPersonalData());
              })),
          Column(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
            Text("Hi,${Global.userName}", style: TextStyle(fontSize: 20, color: MyColor.White, fontWeight: FontWeight.bold)),
            Text("ID:${Global.userId}", style: TextStyle(fontSize: 12, color: MyColor.White)),
          ])
        ]));
  }
}

class ItemMenu extends StatelessWidget {
  String icon;
  String text;
  Function()? onTap;

  ItemMenu(this.icon, this.text, {this.onTap});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: onTap,
        child: Container(
            child: Row(children: [
          LocalImage(icon, width: 20),
          Container(width: 16, height: 40),
          Expanded(child: Text(text, style: TextStyle(fontSize: 14, color: MyColor.C7D))),
          LocalImage("right_arrow.png", width: 12)
        ])));
  }
}

